<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:skos="http://www.w3.org/2004/02/skos/core#"
    xmlns:java="http://xml.apache.org/xslt/java">
    <xsl:output method="xml" indent="yes" encoding="UTF-8" />

    <xsl:template match="/">
        <add>
            <xsl:for-each select="/rdf:RDF/skos:Concept">
            <doc>
                <field name="uri"><xsl:value-of select="@rdf:about" /></field>
                <field name="id"><xsl:value-of select="./dc:identifier" /></field>
                <xsl:for-each select="./skos:prefLabel">
                    <field>
                        <xsl:attribute name="name"><xsl:value-of select="@xml:lang" />PrefLabel</xsl:attribute>
                        <xsl:value-of select="." />
                    </field>
                </xsl:for-each>
                <xsl:for-each select="./skos:altLabel">
                    <field>
                        <xsl:attribute name="name"><xsl:value-of select="@xml:lang" />AltLabel</xsl:attribute>
                        <xsl:value-of select="." />
                    </field>
                </xsl:for-each>
                <xsl:for-each select="./skos:narrower">
                    <field name="narrower">
                        <xsl:value-of select="@rdf:resource" />
                    </field>
                </xsl:for-each>
                <xsl:for-each select="./skos:related">
                    <field name="related">
                        <xsl:value-of select="@rdf:resource" />
                    </field>
                </xsl:for-each>
                <xsl:choose>
                <xsl:when test="./skos:broader">
                    <xsl:for-each select="./skos:broader">
                        <field name="broader">
                            <xsl:value-of select="@rdf:resource" />
                        </field>
                        <xsl:variable name="path"><xsl:call-template name="path">
                                <xsl:with-param name="parent" select="./@rdf:resource" />
                            </xsl:call-template>/<xsl:value-of select="../dc:identifier" /></xsl:variable>
                        <field name="path"><xsl:value-of select="$path" /></field>
                        <field name="level"><xsl:call-template name="countTokens">
                            <xsl:with-param name="string" select="$path" />
                            <xsl:with-param name="token" select="'/'" />
                            <xsl:with-param name="result" select="0" />
                          </xsl:call-template></field>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <field name="path">/<xsl:value-of select="./dc:identifier" /></field>
                </xsl:otherwise>
                </xsl:choose>
            </doc>
            </xsl:for-each>
        </add>
    </xsl:template>
    
    <xsl:template name="countTokens">
        <xsl:param name="string" />
        <xsl:param name="token" />
        <xsl:param name="result" />

        <xsl:choose>
          <xsl:when test="contains($string, $token)">
            <xsl:call-template name="countTokens">
              <xsl:with-param name="string"
        select="substring-after($string, $token)" />
              <xsl:with-param name="token" select="$token"/>
              <xsl:with-param name="result" select="$result + 1"/>
            </xsl:call-template>  
          </xsl:when>
          <xsl:otherwise>
             <xsl:value-of select="$result" />
          </xsl:otherwise>
        </xsl:choose>   
   
    </xsl:template>
    <xsl:template name="path">
        <xsl:param name="parent" />
        <xsl:variable name="c_parent" select="/rdf:RDF/skos:Concept[@rdf:about=$parent]/skos:broader/@rdf:resource" />
        <xsl:if test="$c_parent">
            <xsl:call-template name="path">
                <xsl:with-param name="parent" select="$c_parent" />
            </xsl:call-template>
        </xsl:if>/<xsl:value-of select="/rdf:RDF/skos:Concept[@rdf:about=$parent]/dc:identifier" /></xsl:template>

</xsl:stylesheet>
